package com.martin.ads.omoshiroilib.glessential;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.imageim.yimirror.common.EnviromentConfig;
import com.imageim.yimirror.common.util.LogUtil;

/**
 * Created by Ads on 2017/2/14.
 */

public class GLRootView extends GLSurfaceView {
    private int surfaceWidth;
    private int surfaceHeight;
    private double surfaceRatio = 16.0f/9; // 默认值，通过setAspectRatio修改
//    private double surfaceRatio = 4.0f/3;

    private static final String TAG = "GLRootView";

    private int fixedWidth;
    private int fixedHeight;

    public void setFixedWidth(int fixedWidth) {
        this.fixedWidth = fixedWidth;
    }

    public void setFixedHeight(int fixedHeight) {
        this.fixedHeight = fixedHeight;
    }

    public GLRootView(Context context) {
        super(context);
    }

    public GLRootView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setAspectRatio(int width, int height) {
        if (width < 0 || height < 0) {
            throw new IllegalArgumentException("Size cannot be negative.");
        }
        if (EnviromentConfig.IS_LANDSCAPE) {
            surfaceWidth = width;
            surfaceHeight = height;
        } else {
            surfaceWidth = height;
            surfaceHeight = width;
        }

        LogUtil.d(TAG, "surfaceWidth=" + surfaceWidth + " " + "surfaceHeight=" + surfaceHeight);

        surfaceRatio = (double) surfaceWidth / surfaceHeight;
        RelativeLayout.LayoutParams layoutParams = null;
        if (fixedWidth == 0) {
            layoutParams = new RelativeLayout.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.MATCH_PARENT);
            //        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        } else {
            layoutParams = new RelativeLayout.LayoutParams(fixedWidth, fixedHeight);
            //        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        }
        setLayoutParams(layoutParams);
        requestLayout();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        if (0 == surfaceWidth || 0 == surfaceHeight) {
            setMeasuredDimension(width, height);
        } else {
            if (width < height * surfaceRatio) {
                setMeasuredDimension(width, (int) (width / surfaceRatio));
            } else {
                setMeasuredDimension((int) (height * surfaceRatio), height);
            }
        }
    }
}
